<?php
	namespace app\controllers\first_pra;
	use yii\web\Controller;
	use app\models\first_pra\article;
	class ShowController extends Controller{
		public $enableCsrfValidation = false; 
		public $layout = 'test/common';
		public function actionIndex(){
			$article = article::find();
			$pagination = new \yii\data\Pagination(['totalCount'=>$article->count(),'pageSize'=>4]);
			$data = $article->offset($pagination->offset)->limit($pagination->limit)->asArray()->all();
			$widget =  \yii\widgets\LinkPager::widget([
					'pagination'=>$pagination,
					'options'=>[
						'class'=>'pagination pagination-sm'
					]
				]);
			return $this->render('index',['data'=>$data,'widget'=>$widget]);
			
		}
		public function actionAdd(){
			//$model = 'article';
			$model = [
				'title' => null,
				'description' =>null,
				'content' =>null,
				'flag' =>null,
				'status' =>null,
				'count' =>null
			];
			return $this->render('add',['model'=>$model]);
		}
		public function actionEdit(){
			$id = \yii::$app->request->get('id');
			$model = article::find()->where(['id'=>$id])->asArray()->one();
			return $this->render('edit',['model'=>$model]);
		}
		public function actionDelete(){

		}
		public function actionTest(){
			$model = new article;
			return $this->render('form2',['model'=>$model]);
		}
		//处理数据
		public function actionLoad(){
			$post['article'] = \yii::$app->request->post();
			$article = new article();
			$article->load($post);
			$article->validate();
			if(!$article->getErrors()){
				$article->save();
				echo "<script type='text/javascript'>alert('上传成功');</script>";
			}
			else
				echo "<script type='text/javascript'>alert('上传失败');</script>";
			return $this->render('add');
		}
		//处理ajax
		public function actionAjax(){
			$string=null;
			$post = \yii::$app->request->post();//新建一个post请求
			$key = $post['key'];
			$data['article'][$key] = $post['value']; 
			$article = new article();//新建表对象		
			$article->load($data);//数据读入
			$article->validate();//开启验证提示
			$error = $article->getErrors($key);//获取对应key的error值
			return json_encode($error);
		}
	}
?>